package JavaSE.d6;

import java.text.SimpleDateFormat;
import java.util.Date;

/*
如何使用interrupt中断线程
 */
public class Test5 {
    public static void main(String[] args) throws InterruptedException {
        CG cg = new CG();
        Thread thread = new Thread(cg);
        thread.start();
        Thread.sleep(5000);
        thread.interrupt();
        System.out.println("关闭时钟");
    }
}
class CG implements Runnable{
    @Override
    public void run() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");
        while (true){
            if(Thread.currentThread().isInterrupted()){
                break;
            }
            Date date = new Date();
            System.out.println(simpleDateFormat.format(date));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
}